package com.shengde.jinwawaforleader.model.homePage;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.activity.LoginActivity;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProAttendanceRecoder;
import com.shengde.jinwawaforleader.utils.DateUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;


/**
 * 班级打卡详情
 * Created by Administrator on 2016/8/31 0031.
 */
public class AttendanceClassActivity extends BaseActivity implements AdapterView.OnItemClickListener {
	private static final String TAG = "AttendanceClassActivity";

	private static final int FINSH = 1;
	private TextView tv_title;
	private TextView tv_back;

	private ListView lv_daka_recode;

	private String classid;
	private String date;
	private List<ProAttendanceRecoder.StuBean> stuBeen;
	private List<ProAttendanceRecoder.TeacherBean> teacherBeen;
	private DakaRecodeAdapter dakaRecodeAdapter;
	private LinearLayout loading_main;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (dakaRecodeAdapter == null) {
						dakaRecodeAdapter = new DakaRecodeAdapter(AttendanceClassActivity.this, stuBeen, teacherBeen);
						lv_daka_recode.setAdapter(dakaRecodeAdapter);

					}else {
						dakaRecodeAdapter.notifyDataSetChanged();
					}
					loading_main.setVisibility(View.GONE);

					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.attendance_class_activity);
	}

	@Override
	public void initView() {
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_back = (TextView) findViewById(R.id.tv_back);
		lv_daka_recode = (ListView) findViewById(R.id.lv_daka_recode);
		loading_main = (LinearLayout) findViewById(R.id.loading_main);

	}

	@Override
	public void initData() {
		classid = getIntent().getStringExtra("classid");
		date = getIntent().getStringExtra("date");
		tv_title.setText(getIntent().getStringExtra("className"));
		showTeacherList(date);


	}

	/**
	 * 显示老师打卡信息列表的网络请求
	 * @param date
	 */
	private void showTeacherList(String date) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/kqList";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/kqList";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("classid", classid);
		params.addParameter("date", date);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProAttendanceRecoder pro_attendance_recoder = new Gson().fromJson(result, ProAttendanceRecoder.class);
				Log.i(TAG, "onSuccess:  " + pro_attendance_recoder.getCode());
				if (pro_attendance_recoder != null && pro_attendance_recoder.getCode() == 0) {
					stuBeen = pro_attendance_recoder.getStu();
					teacherBeen = pro_attendance_recoder.getTeacher();
					Log.i(TAG, "onSuccess: s " +stuBeen.size());
					Log.i(TAG, "onSuccess:  t" +teacherBeen.size());
					mHandler.sendEmptyMessage(FINSH);

				}

			}
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
			}

			@Override
			public void onCancelled(CancelledException cex) {
			}

			@Override
			public void onFinished() {
			}
		});

	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		lv_daka_recode.setOnItemClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position <=teacherBeen.size()+1){
			return;
		}else {
			position = position-teacherBeen.size()-2;
		}

		final Dialog dialog = new Dialog(this, R.style.dialog);
		View contentView = LayoutInflater.from(this).inflate(
				R.layout.dialog_show_time, null);
		dialog.setContentView(contentView);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		LinearLayout table_time_show_view = (LinearLayout) contentView.findViewById(R.id.table_time_show);
		TextView time_1 = (TextView) table_time_show_view.findViewById(R.id.time_1);
		TextView time_2 = (TextView) table_time_show_view.findViewById(R.id.time_2);
		TextView time_3 = (TextView) table_time_show_view.findViewById(R.id.time_3);
		TextView time_4 = (TextView) table_time_show_view.findViewById(R.id.time_4);
		TextView time_5 = (TextView) table_time_show_view.findViewById(R.id.time_5);
		TextView time_6 = (TextView) table_time_show_view.findViewById(R.id.time_6);
		Log.i(TAG, "onItemClick: postion = " + position);
		Log.i(TAG, "onItemClick: time = " + stuBeen.get(position).getTime1());
		if (stuBeen.get(position).getTime1() == null ||"".equals(stuBeen.get(position).getTime1())){
			time_1.setText("00:00");
		}else {
			time_1.setText(DateUtil.getDateT(stuBeen.get(position).getTime1()+""));
		}
		if (stuBeen.get(position).getTime2() == null ||"".equals(stuBeen.get(position).getTime2())){
			time_2.setText("00:00");
		}else {
			time_2.setText(DateUtil.getDateT(stuBeen.get(position).getTime2()+""));
		}
		if (stuBeen.get(position).getTime3() == null ||"".equals(stuBeen.get(position).getTime3())){
			time_3.setText("00:00");
		}else {
			time_3.setText(DateUtil.getDateT(stuBeen.get(position).getTime3()+""));
		}
		if (stuBeen.get(position).getTime4() == null ||"".equals(stuBeen.get(position).getTime4())){
			time_4.setText("00:00");
		}else {
			time_4.setText(DateUtil.getDateT(stuBeen.get(position).getTime4()+""));
		}
		if (stuBeen.get(position).getTime5() == null ||"".equals(stuBeen.get(position).getTime5())){
			time_5.setText("00:00");
		}else {
			time_5.setText(DateUtil.getDateT(stuBeen.get(position).getTime5()+""));
		}
		if (stuBeen.get(position).getTime6() == null ||"".equals(stuBeen.get(position).getTime6())){
			time_6.setText("00:00");
		}else {
			time_6.setText(DateUtil.getDateT(stuBeen.get(position).getTime6()+""));
		}
	}
}
